package ruixinyi.experiment2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ruixinyi.experiment2.entity.Course;
import ruixinyi.experiment2.entity.Grade;
import ruixinyi.experiment2.entity.Student;
import ruixinyi.experiment2.service.CourseService;
import ruixinyi.experiment2.service.GradeService;
import ruixinyi.experiment2.service.StudentService;
import java.util.List;
@RestController
@RequestMapping("/api")
public class StudentGradeController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private GradeService gradeService;
    // 学生信息管理
    @GetMapping("/students/listAll")
    public List<Student> getAllStudents() {
        return studentService.findAll();
    }

    @GetMapping("/students/getById/{studentId}")
    public Student getStudentById(@PathVariable Integer studentId) {
        return studentService.findById(studentId);
    }
    @GetMapping("/students/getGrades/{studentId}")
    public List<Grade> getGradesByStudentId(@PathVariable Integer studentId) {
        return gradeService.findByStudentId(studentId);
    }
    @PostMapping("/students/add")
    public Student addStudent(@RequestBody Student student) {
        return studentService.save(student);
    }
    // 课程信息管理
    @GetMapping("/courses/listAll")
    public List<Course> getAllCourses() {
        return courseService.findAll();
    }

    @GetMapping("/courses/getById/{courseId}")
    public Course getCourseById(@PathVariable Integer courseId) {
        return courseService.findById(courseId);
    }

    @PostMapping("/courses/add")
    public Course addCourse(@RequestBody Course course) {
        return courseService.save(course);
    }

    // 成绩管理
    @GetMapping("/grades/listAll")
    public List<Grade> getAllGrades() {
        return gradeService.findAll();
    }

    @GetMapping("/grades/getByCourseId/{courseId}")
    public List<Grade> getGradesByCourseId(@PathVariable Integer courseId) {
        return gradeService.findByCourseId(courseId);
    }

    @PutMapping("/grades/update")
    public Grade updateGrade(@RequestBody Grade grade) {
        return gradeService.save(grade);
    }

    @GetMapping("/grades/getByRange/{minScore}/{maxScore}")
    public List<Grade> getGradesByRange(@PathVariable Double minScore, @PathVariable Double maxScore) {
        return gradeService.findByScoreRange(minScore, maxScore);
    }
}